package com.fjb.controller.login;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.enums.user.UserType;
import com.fjb.controller.base.BaseController;
import com.fjb.pojo.login.LoginUserInfo;
import com.fjb.pojo.user.SysUser;
import com.fjb.service.user.SysUserService;

/**
 * @Description:登录
 * @author hemiao
 * @time:2019年5月1日 下午10:38:08
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * @Description:登陆验证
	 * @param request
	 * @return
	 * ModelAndView
	 * @exception:
	 * @author: hemiao
	 * @time:2019年5月1日 下午10:40:00
	 */
	@RequestMapping(value = "/verify")
	public ModelAndView loginVerify(HttpServletRequest request,HttpServletResponse response,RedirectAttributes ra) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/login");
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		// 手机号	
		String phone = request.getParameter("phone");
		if(StringUtils.isBlank(phone)) {
			httpCode.setMsg("手机号不能为空");
			ra.addFlashAttribute("error", httpCode.getMsg());
			return mv;
		}	
//		Cookie phoneCookie = new Cookie(BaseController.LOGIN_PHONE, phone);
//		// 过期时间为1小时 60*60 60*60*24*15 = 15天
//		phoneCookie.setMaxAge(60 * 60 * 24 * 15);
//		phoneCookie.setPath("/");	
//		response.addCookie(phoneCookie);
		
		// 密码
		String loginPassword = request.getParameter("password");
		if(StringUtils.isBlank(loginPassword)) {
			httpCode.setMsg("密码不能为空");
			ra.addFlashAttribute("error", httpCode.getMsg());
			return mv;	
		}
		QueryWrapper<SysUser> userWrapper = new QueryWrapper<SysUser>();
		userWrapper.eq("phone", phone);
		SysUser sysUser = sysUserService.getOne(userWrapper);
		if(sysUser==null) {
			httpCode.setMsg("用户信息不存在");
			ra.addFlashAttribute("error", httpCode.getMsg());
			return mv;
		}
		String password = sysUser.getPassword();
		if(!password.equals(loginPassword)) {
			httpCode.setMsg("账号或密码不正确");
			ra.addFlashAttribute("error", httpCode.getMsg());
			return mv;
		}
		Integer userType = sysUser.getUserType();	
		if(!userType.equals(UserType.TYPE_1000.getCode())) {	
			httpCode.setMsg("登录权限不足");
			ra.addFlashAttribute("error", httpCode.getMsg());
			return mv;
		}
		
		LoginUserInfo loginUserInfo = new LoginUserInfo();
		loginUserInfo.setSysUser(sysUser);
		HttpSession httpSession = request.getSession();
		httpSession.setMaxInactiveInterval(120*60);	
		httpSession.setAttribute(LOGIN_USER_INFO, loginUserInfo);	
		mv.setViewName("redirect:/index");
		// 添加登录日志
		return mv;
	}
	
	/**
	 * @Description:登录退出
	 * @param request
	 * @return
	 * String
	 * @exception:
	 * @author: hemiao
	 * @time:2019年5月1日 下午10:37:33
	 */
	@RequestMapping(value="/out")
	public String loginOut(HttpServletRequest request) {
		return "redirect:/login";
	}
}
